package com.bbs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbs.dto.Result;
import com.bbs.dto.UserDTO;
import com.bbs.entity.Follow;
import com.bbs.mapper.FollowMapper;
import com.bbs.service.FollowService;
import com.bbs.service.UserService;
import com.bbs.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.security.Provider;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.bbs.util.constant.FOLLOW_USER_ID;

@Service
@Slf4j
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {
    @Autowired
    FollowMapper followMapper;
    @Autowired
    UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        //获取用户id
        Long userId = UserHolder.getUser().getId();
        //如果关注了，该查询结果能返回Id
      Follow one = query().eq("user_id",userId).eq("follow_user_id", followUserId).one();
        if(!isFollow){//没有关注
          //关注
          Follow follow=new Follow();
          follow.setUserId(userId);
          follow.setFollowUserId(followUserId);
            boolean save = save(follow);
            //set集合  key:当前用户id value[]:关注的用户

            if(save){
                String key=FOLLOW_USER_ID+userId;
                //添加到缓存中
                stringRedisTemplate.opsForSet().add(key,followUserId.toString());
            }
          log.info("关注者id:{},被关注者id:{}",userId,followUserId);
      }else{
          //取关
            Long id = one.getId();
            boolean remove = removeById(id);
            String key=FOLLOW_USER_ID+userId;
            //从缓存中删除
            if(remove)stringRedisTemplate.opsForSet().remove(key,followUserId.toString());

            log.info("取关操作");
      }
        return Result.ok();
    }

    @Override
    public Result judgeFollow(Long followUserId) {
        //也可以到缓存中查询
     Long userId=UserHolder.getUser().getId();
        Long count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        return Result.ok(count>0);
    }

    @Override
    public Result followCommons(Long id) {
      //获取当前用户id
        Long userId = UserHolder.getUser().getId();
        String key=FOLLOW_USER_ID+userId;
                String otherKey=FOLLOW_USER_ID+id;
        //获取两个关注Set集合
        //求交集
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key, otherKey);
        if(intersect==null)return Result.ok(Collections.emptyList());//返回空集合
        //转化为Long
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        //根据交集id查询用户   //映射为DTO
        List<UserDTO> userDTOList = userService.listByIds(ids)
                                .stream().map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                                .collect(Collectors.toList());

        //返回
        return Result.ok(userDTOList);
    }
}
